package org.openossad.data;

import com.mxgraph.util.mxUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: fjhidalgo
 * Date: 24/10/11
 * Time: 15:32
 * To change this template use File | Settings | File Templates.
 */
public class CellUserObject
{
    private Element ooCellObject;

    private String comment;
    private String value;
    private String relationId;
    private String relationName;

    private static String commentAttribute      = "comment";
    private static String valueAttribute        = "value";
    private static String relationIdAttribute   = "relationId";
    private static String relationNameAttribute = "relationName";

    public CellUserObject(String value)
    {
        ooCellObject = initCellUserObject(value);
    }

    public CellUserObject(Object value)
    {
        if (value instanceof String) {
            ooCellObject = initCellUserObject((String) value);
        } else if (value instanceof Element) {
            ooCellObject= (Element) value;
        } else {
            ooCellObject = initCellUserObject("");
        }
    }

    private Element initCellUserObject(String value)
    {
        Document doc = mxUtils.createDocument();
        ooCellObject = doc.createElement("ooCellObject");
        setValue(value);
        setComment("");
        setRelationId("");
        setRelationName("");
        return ooCellObject;
    }

    public Object getUserObject()
    {
		return ooCellObject;
    }

    public String getComment()
    {
        return ooCellObject.getAttribute(commentAttribute);
    }

    public String getValue()
    {
        return ooCellObject.getAttribute(valueAttribute);
    }

    public void setComment(String comment)
    {
        this.comment=comment;
        ooCellObject.setAttribute(commentAttribute,comment);
    }

    public void setValue(String value)
    {
        this.value=value;
        ooCellObject.setAttribute(valueAttribute,value);
    }

    public String getRelationId()
    {
        return ooCellObject.getAttribute(relationIdAttribute);
    }

    public void setRelationId(String relationId)
    {
        this.relationId = relationId;
        ooCellObject.setAttribute(relationIdAttribute,relationId);
    }

    public String getRelationName()
    {
        return ooCellObject.getAttribute(relationNameAttribute);
    }

    public void setRelationName(String relationName)
    {
        this.relationName = relationName;
        ooCellObject.setAttribute(relationNameAttribute,relationName);
    }

    public void setUserObject(Object value)
    {
        ooCellObject=(Element)value;
    }

    public void clearRelations()
    {
        setRelationId("");
        setRelationName("");
    }
}
